package top.maof.book.controller;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.maof.book.model.Book;
import top.maof.book.rank.ClickHotRank;
import top.maof.book.rank.CollectionHotRank;
import top.maof.book.rank.LikesHotRank;
import top.maof.book.rank.RankVo;
import top.maof.book.rank.book.*;
import top.maof.book.result.Result;
import top.maof.book.service.BookService;
import top.maof.book.type.HotType;
import top.maof.book.vo.PageVo;

import java.util.List;
import java.util.Set;

/**
 * 排名
 */
@RestController
@RequestMapping("/rank")
@Api("排名")
public class RankController {

    @Autowired
    ClickHotRank clickHotRank;

    @Autowired
    CollectionHotRank collectionHotRank;


    @Autowired
    LikesHotRank likesHotRank;

    @Autowired
    BookService bookService;

    @Autowired
    BookHandleRank bookHandleRank;


    @GetMapping("/book/click")
    public Result<PageVo<RankVo<Book>>> bookClick(BookPage bookPage) {
        bookPage.setHotType(HotType.CLICK);
        PageVo<RankVo<Book>> page = bookHandleRank.page(bookPage);
        return Result.success(page);
    }

    @GetMapping("/book/collection")
    public Result<PageVo<RankVo<Book>>> bookCollection(BookPage bookPage) {
        bookPage.setHotType(HotType.COLLECTION);
        PageVo<RankVo<Book>> page = bookHandleRank.page(bookPage);
        return Result.success(page);
    }

    @GetMapping("/book/likes")
    public Result<PageVo<RankVo<Book>>> bookLikes(BookPage bookPage) {
        bookPage.setHotType(HotType.LIKES);
        PageVo<RankVo<Book>> page = bookHandleRank.page(bookPage);
        return Result.success(page);
    }




    @GetMapping("/click")
    @Deprecated
    public Result<List<Book>> click(Integer type, Integer page, Integer pageSize, Integer year, Integer month,
                                    Integer week, Integer day) throws Exception {
        int start = (page - 1) * pageSize;
        int end = page * pageSize - 1;
        if (end <= start) {
            return Result.response_403();
        }
        Set<Integer> set = null;
        switch (type) {
            case 0:
                set = clickHotRank.totalRank(start, end);
                break;
            case 1:
                set = clickHotRank.yearWeek(year, start, end);
                break;
            case 2:
                set = clickHotRank.monthRank(year, month, start, end);
                break;
            case 3:
                set = clickHotRank.weekRank(year, month, week, start, end);
                break;
            default:
                set = clickHotRank.dayRank(year, month, week, day, start, end);
                break;
        }
        List<Book> books = bookService.listByIds(set);
        return Result.success(books);
    }


    @GetMapping("/collection")
    @Deprecated
    public Result<List<Book>> collection(Integer type, Integer page, Integer pageSize, Integer year, Integer month,
                                         Integer week, Integer day) throws Exception {
        int start = (page - 1) * pageSize;
        int end = page * pageSize - 1;
        if (end <= start) {
            return Result.response_403();
        }
        Set<Integer> set = null;
        switch (type) {
            case 0:
                set = collectionHotRank.totalRank(start, end);
                break;
            case 1:
                set = collectionHotRank.yearWeek(year, start, end);
                break;
            case 2:
                set = collectionHotRank.monthRank(year, month, start, end);
                break;
            case 3:
                set = collectionHotRank.weekRank(year, month, week, start, end);
                break;
            default:
                set = collectionHotRank.dayRank(year, month, week, day, start, end);
                break;
        }
        List<Book> books = bookService.listByIds(set);
        return Result.success(books);
    }


    @GetMapping("/likes")
    @Deprecated
    public Result<List<Book>> likes(Integer type, Integer page, Integer pageSize, Integer year, Integer month,
                                    Integer week, Integer day) throws Exception {
        int start = (page - 1) * pageSize;
        int end = page * pageSize - 1;
        if (end <= start) {
            return Result.response_403();
        }
        Set<Integer> set = null;
        switch (type) {
            case 0:
                set = likesHotRank.totalRank(start, end);
                break;
            case 1:
                set = likesHotRank.yearWeek(year, start, end);
                break;
            case 2:
                set = likesHotRank.monthRank(year, month, start, end);
                break;
            case 3:
                set = likesHotRank.weekRank(year, month, week, start, end);
                break;
            default:
                set = likesHotRank.dayRank(year, month, week, day, start, end);
                break;
        }
        List<Book> books = bookService.listByIds(set);
        return Result.success(books);
    }
}
